// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import api from '../api/index.js'

Vue.use(Vuex); //vue的插件机制
const store = new Vuex.Store({
	//全局数据
	state: {
		token: "",
		userInfo: "",
		permissionName: "",
		url: "http://192.168.10.130:8088",
		// url: "http://mail.rlhotel.cn",
		isAuth: false,
		//商品分类
		goodsGroup: [],
		//购物车商品列表
		cartGoodsList: [],
		//客户信息
		customer: {},
		//购车选中商品列表
		optionGoodsList: [],
		//购物车商品信息
		cartGoods: {}

	},
	//全局计算属性
	getters: {

	},
	//修改数据的方法
	mutations: {
		//保存登录信息
		saveLoginInfo(state, info) {
			state.isAuth = true
			state.token = info.token
			state.permissionName = info.permissionName
			//存储到到uniapp提供持久化数据仓库存储到仓库中
			uni.setStorage({
				key: "isAuth",
				data: true
			})
			uni.setStorageSync('token', info.token);
			uni.setStorage({
				key: "permission",
				data: info.permissionName
			})
			uni.setStorage({
				key: "token",
				data: info.token
			})
		},
		//保存用户信息
		saveUserInfo(state, user) {
			state.userInfo = user
			uni.setStorage({
				key: "userInfo",
				data: user
			})
		},
		//保存商品分类
		saveGoodsGroup(state, info) {
			state.goodsGroup = info
			uni.setStorage({
				key: "goodsGroup",
				data: info
			})
		},
		//保存购物车中的商品列表
		saveCartList(state, value) {
			state.cartGoodsList = value
			uni.setStorage({
				key: "cart",
				data: value
			})
		},
		//保存用户个人资料信息
		saveCustomer(state, value) {
			state.customer = value
			uni.setStorage({
				key: "customer",
				data: value
			})
		},
		//保存选中购车商品列表
		saveOptionGoodsList(state, value) {
			state.optionGoodsList = value
			uni.setStorage({
				key: "optionGoods",
				data: value
			})
		},
		//保存购物车选中的商品信息
		saveCartGoods(state, value) {
			state.cartGoods = value
			uni.setStorage({
				key: "cartGoods",
				data: value
			})
		}
	},
	//全局异步操作方法
	actions: {
		//发送请求获取购物车所有的商品列表
		async getCartGoodsList(context) {
			//发送请求
			const {
				status,
				data,
				msg
			} = await api.cart.queryCustomerCartList()
			if (status == 200) {
				for (let i = 0; i < data.length; i++) {
					data[i].imgUrl = this.state.url + data[i].imgUrl
				}
				// console.log(goodsData)
				//将购物车中的商品数据保存到vex的state中
				context.commit('saveCartList', data)
			}

		},
		//发送请求获取购物车选中的所有商品信息
		async queryCartGoods(context) {
			const {
				status,
				data,
				msg
			} = await api.cart.queryCartProductInfo()
			if (status == 200 && data != null) {
				if (data.cartList.length > 0) {
					for (let i = 0; i < data.cartList.length; i++) {
						data.cartList[i].imgUrl = this.state.url + data.cartList[i].imgUrl
					}
					data.goodsCount = data.cartList.length
					data.isChecked = true
				} else {
					data.isChecked = false
				}
				context.commit('saveCartGoods', data)
			}
		},
		//发送请求获取购物车选中的所有商品列表
		async queryOptionGoodsList(context) {
			const goodsList = []
			const {
				status,
				data,
				msg
			} = await api.cart.queryCartProductInfo()
			if (status == 200 && data != null) {
				if (data.cartList.length > 0) {
					for (let i = 0; i < data.cartList.length; i++) {
						data.cartList[i].imgUrl = this.state.url + data.cartList[i].imgUrl
					}
					if (data.cartList.length > 4) {
						goodsList.push(data.cartList[0])
						goodsList.push(data.cartList[1])
						goodsList.push(data.cartList[2])
						goodsList.push(data.cartList[3])
						context.commit("saveOptionGoodsList", goodsList)
					} else {
						context.commit("saveOptionGoodsList", data.cartList)

					}


				}
				// context.commit('saveCartGoods', data)
			}
		}
	}
})
export default store